﻿// facecat_easyx.cpp : 定义应用程序的入口点。
//

#include <graphics.h>		// 引用 EasyX 绘图库头文件
#include "Win32\\EasyXPaint.h"
#include "Win32\\EasyXHost.h"
#include "MainFrame.h"
#include <thread>

/**
* 设备对接
*/
EasyXHost* m_host;
/**
* 公共方法库
*/
FCNative* m_native;
/*
* 绘图对象
*/
EasyXPaint* m_paint;

typedef void deal_mousefunc(MOUSEMSG);

std::unique_ptr<std::thread> listening_mouse(deal_mousefunc* func) {
	std::unique_ptr<std::thread> u_ptr(new std::thread([func]() {
		while (1)
		{
			while (!MouseHit()) std::this_thread::yield();
			func(GetMouseMsg());
		}
		}));
	u_ptr->detach();
	return u_ptr;
}

void deal_mouse_func(MOUSEMSG mouse_message) {
	if (mouse_message.uMsg == WM_MOUSEMOVE)
	{
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		if (m_native->m_touchDownView) {
			touchInfo.m_clicks = 1;
			touchInfo.m_firstTouch = true;
		}
		touchInfo.m_firstPoint = pt;
		m_native->onTouchMove(touchInfo);
	}
	else if (mouse_message.uMsg == WM_LBUTTONDOWN) {
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 1;
		touchInfo.m_firstTouch = true;
		touchInfo.m_firstPoint = pt;
		FCView* lastFocusedView = m_native->getFocusedView();
		m_native->onTouchDown(touchInfo);
	}
	else if (mouse_message.uMsg == WM_LBUTTONUP) {
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 1;
		touchInfo.m_firstTouch = true;
		touchInfo.m_firstPoint = pt;
		m_native->onTouchUp(touchInfo);
	}
	else if (mouse_message.uMsg == WM_LBUTTONDBLCLK) {
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 2;
		touchInfo.m_firstTouch = true;
		touchInfo.m_firstPoint = pt;
		m_native->onTouchDown(touchInfo);
	}
	else if (mouse_message.uMsg == WM_RBUTTONDOWN) {
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 1;
		touchInfo.m_secondTouch = true;
		touchInfo.m_secondPoint = pt;
		FCView* lastFocusedView = m_native->getFocusedView();
		m_native->onTouchDown(touchInfo);
	}
	else if (mouse_message.uMsg == WM_RBUTTONUP) {
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 1;
		touchInfo.m_secondTouch = true;
		touchInfo.m_secondPoint = pt;
		m_native->onTouchUp(touchInfo);
	}
	else if (mouse_message.uMsg == WM_MOUSEWHEEL) {
		FCPoint pt = { mouse_message.x, mouse_message.y };
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 0;
		touchInfo.m_firstTouch = false;
		touchInfo.m_firstPoint = pt;
		touchInfo.m_delta = mouse_message.wheel;
		m_native->onTouchWheel(touchInfo);
	}
}

static void WINAPI TimerCallback(HWND hWnd, UINT uMsg, UINT_PTR ptr, DWORD dw)
{
	if (m_native) {
		EasyXHost* host = dynamic_cast<EasyXHost*>(m_native->getHost());
		host->onTimer();
	}
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd = initgraph(1000, 700);	
	m_native = new FCNative;
	String appPath = FCTran::stringToString(FCTran::getAppPath());
	m_native->setResourcePath(appPath + L"\\");
	m_paint = new EasyXPaint();
	m_native->setPaint(m_paint);
	EasyXHost* host = new EasyXHost;
	host->setNative(m_native);
	m_native->setHost(host);
	m_host = host;
	MainFrame* mainFrame = new MainFrame;
	mainFrame->setNative(m_native);
	mainFrame->loadFile(appPath + L"\\MainFrame.xml", 0);
	FCSize newSize = { 1000, 700 };
	m_native->setSize(newSize);
	m_native->update();
	m_native->invalidate();
	SetTimer(hWnd, 1, 10, TimerCallback);
	auto thread2 = listening_mouse(deal_mouse_func);
	BeginBatchDraw();
	while (1);
	EndBatchDraw();
    closegraph();
    return 0;
}

